﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace CPPEI.Coolzon.UI.WinForm
{
    public partial class ActiveXWrapper : UserControl
    {
        #region WinAPI
        [StructLayout(LayoutKind.Sequential)]
        public struct RECT { public int left; public int top; public int right; public int bottom; }
        //声明一个API函数  
        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
        private static extern bool BitBlt(
            IntPtr hdcDest, // 目标 DC的句柄  
            int nXDest,
            int nYDest,
            int nWidth,
            int nHeight,
            IntPtr hdcSrc,  // 源DC的句柄  
            int nXSrc,
            int nYSrc,
            System.Int32 dwRop  // 光栅的处理数值  

            );
        [DllImport("user32.dll")]
        public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateDC(
         string lpszDriver,         // driver name驱动名
         string lpszDevice,         // device name设备名
         string lpszOutput,         // not used; should be NULL
         IntPtr lpInitData   // optional printer data
         );
        [DllImport("gdi32.dll")]
        public static extern int BitBlt(
         IntPtr hdcDest, // handle to destination DC目标设备的句柄
         int nXDest,   // x-coord of destination upper-left corner目标对象的左上角的X坐标
         int nYDest,   // y-coord of destination upper-left corner目标对象的左上角的Y坐标
         int nWidth,   // width of destination rectangle目标对象的矩形宽度
         int nHeight, // height of destination rectangle目标对象的矩形长度
         IntPtr hdcSrc,   // handle to source DC源设备的句柄
         int nXSrc,    // x-coordinate of source upper-left corner源对象的左上角的X坐标
         int nYSrc,    // y-coordinate of source upper-left corner源对象的左上角的Y坐标
         UInt32 dwRop   // raster operation code光栅的操作值
         );
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleDC(
         IntPtr hdc // handle to DC
         );
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleBitmap(
         IntPtr hdc,         // handle to DC
         int nWidth,      // width of bitmap, in pixels
         int nHeight      // height of bitmap, in pixels
         );
        [DllImport("gdi32.dll")]
        public static extern IntPtr SelectObject(
         IntPtr hdc,           // handle to DC
         IntPtr hgdiobj    // handle to object
         );
        [DllImport("gdi32.dll")]
        public static extern int DeleteDC(
         IntPtr hdc           // handle to DC
         );
        [DllImport("user32.dll")]
        public static extern bool PrintWindow(
         IntPtr hwnd,                // Window to copy,Handle to the window that will be copied.
         IntPtr hdcBlt,              // HDC to print into,Handle to the device context.
         UInt32 nFlags               // Optional flags,Specifies the drawing options. It can be one of the following values.
         );
        [DllImport("user32.dll")]
        public static extern IntPtr GetWindowDC(
         IntPtr hwnd
         );
        #endregion
        private Control axHost;
        public ActiveXWrapper()
        {
            InitializeComponent();
        }

        public ActiveXWrapper(object activeXObject)
        {
            InitializeComponent();
            axHost = (Control)activeXObject;
            this.Controls.Add(axHost);
            //this.AutoScroll = true;
            //this.VerticalScroll.Visible = false;
            //this.HorizontalScroll.Visible = false;
        }

        public void SetContentScrollOffset(double hOffset, double vOffset)
        {
            if (axHost != null)
            {
                //this.AutoScrollOffset = new Point(Convert.ToInt32(hOffset), Convert.ToInt32(vOffset));
                int actualVOffset = (int)vOffset;
                int actualHOffset = (int)hOffset;

                int maxHOffset = axHost.Width - this.Width;
                int maxVOffset = axHost.Height - this.Height;

                if (actualVOffset > maxVOffset)
                {
                    actualVOffset = maxVOffset;
                }
                if (actualHOffset > maxHOffset)
                {
                    actualHOffset = maxHOffset;
                }

                axHost.Left = -actualHOffset;
                axHost.Top = -actualVOffset;

                //if (actualVOffset > this.VerticalScroll.Maximum)
                //{
                //    actualVOffset = this.VerticalScroll.Maximum;
                //}
                //else if (actualVOffset < this.VerticalScroll.Minimum)
                //{
                //    actualVOffset = this.VerticalScroll.Minimum;
                //}

                //if (actualHOffset > this.HorizontalScroll.Maximum)
                //{
                //    actualHOffset = this.HorizontalScroll.Maximum;
                //}
                //else if (actualHOffset < this.HorizontalScroll.Minimum)
                //{
                //    actualHOffset = this.HorizontalScroll.Minimum;
                //}

                //this.VerticalScroll.Value = actualVOffset;
                //this.HorizontalScroll.Value = actualHOffset;
            }
        }

        public void SetContentSize(double width, double height)
        {
            axHost.Width = Convert.ToInt32(width);
            axHost.Height = Convert.ToInt32(height);
        }

        public void SetWrapperSize(double width, double height)
        {
            Width = Convert.ToInt32(width);
            Height = Convert.ToInt32(height);
        }


        /// <summary>
        /// 投影显示到图像上
        /// </summary>
        /// <returns></returns>
        public Bitmap ProjectionToImage()
        {
            Bitmap bit = GetWindowCapture(axHost.Handle);
            return bit;
        }

        public Bitmap GetWindowCapture(IntPtr hWnd)
        {
            IntPtr hscrdc = GetWindowDC(hWnd);
            RECT windowRect = new RECT();
            GetWindowRect(hWnd, ref windowRect);
            int width = windowRect.right - windowRect.left;
            int height = windowRect.bottom - windowRect.top;
            IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, width, height);
            IntPtr hmemdc = CreateCompatibleDC(hscrdc);
            SelectObject(hmemdc, hbitmap);
            PrintWindow(hWnd, hmemdc, 0);
            Bitmap bmp = Bitmap.FromHbitmap(hbitmap);
            DeleteDC(hscrdc);//删除用过的对象
            DeleteDC(hmemdc);//删除用过的对象
            return bmp;
        }
    }
}
